<?php
/**
 * Created by PhpStorm.
 * User: hp
 * Date: 2019/3/29
 * Time: 17:57
 */

namespace App\Http\Controllers\VipChannel\Account;

use App\Http\Controllers\VipChannel\BaseController;
use App\Modules\Channel\Models\Channel;
use App\Modules\Channel\Models\ChannelUser;
use App\Modules\User\Models\VipChannelUsers;
use DB;
use Illuminate\Http\Request;

class AccountController extends BaseController
{
    public function getAccountInfo(Request $request)
    {
        $result = [];
        if ($this->getManageType($request) != 'manage') {
            return response()->error('NOT_VIP_ACCOUNT');
        } else {
            $channel_user_ids = $this->getChannelUsers($request);
            foreach ($channel_user_ids as $channel_user_id) {
                $channel_item = [];
                $channelUser = ChannelUser::getById($channel_user_id);
                $channels = ChannelUser::getChannels($channel_user_id);
                foreach ($channels as $channel) {
                    $channel_item['phone'] = $channelUser->phone;
                    $channel_item['channel_user_id'] = $channel_user_id;
                    $channel_item['distribution_channel_id'] = $channel->id;
                    $channel_item['channel_user_name'] = $channelUser->nickname;
                    $channel_item['distribution_channel_name'] = $channel->site_nick_name;
                    $result[] = $channel_item;
                }
            }
            return response()->success($result);
        }
    }

    public function editDistributionChannelName(Request $request)
    {
        $name = $request->has('name') ? $request->input('name') : '';
        $distribution_channel_id = $request->has('distribution_channel_id') ? $request->input('distribution_channel_id') : '';
        if (!$distribution_channel_id || !$name) {
            return response()->error('PARAM_EMPTY');
        }
        $channel = Channel::getIdById($distribution_channel_id);
        if (!$channel) {
            return response()->error('SITE_NOT_EXIST');
        }

        $channel->site_nick_name = $name;
        $channel->save();
        return response()->success();
    }

    public function editChannelUserName(Request $request)
    {
        $nickname = $request->has('nickname') ? $request->input('nickname') : '';
        $channel_user_id = $request->has('channel_user_id') ? $request->input('channel_user_id') : '';
        if (!$channel_user_id || !$nickname) {
            return response()->error('PARAM_EMPTY');
        }
        $channel_user = ChannelUser::getById($channel_user_id);
        if (!$channel_user) {
            return response()->error('ACCOUNT_NOT_EXIST');
        }

        $channel_user->nickname = $nickname;
        $channel_user->save();
        return response()->success();
    }

    public function addAccount(Request $request)
    {
        $phone = $request->has('phone') ? $request->input('phone') : '';
        $password = $request->has('password') ? $request->input('password') : '';

        if (empty($phone) || empty($password)) {
            return response()->error('PARAM_EMPTY');
        }

        $format = '/^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/';
        if (!preg_match($format, $phone)) {
            return response()->error('PHONE_FORMAT_ERROR');
        }

        $manager_type = $this->getManageType($request);
        if ($manager_type != 'manage') {
            return response()->error('ACCOUNT_TYPE_ERROR');
        }

        $encode_pwd = md5($password . '^-^zhuishuyun^_^');
        $channel_user = ChannelUser::judgeLogin($phone, $encode_pwd);
        if (!$channel_user) {
            return response()->error('LOGIN_VERIFY_ERROR');
        } else {
            $channel_user_id = $channel_user->id;
            $vip_manager_id = $this->getManagerId($request);
            $vipChannel = VipChannelUsers::judgeIsExist($channel_user_id, $vip_manager_id);
            $is_enabled = 1;
            if ($vipChannel) {
                if ($vipChannel->is_enabled == 1) {
                    return response()->error('ALREADY_ADD');
                } else {
                    //如果存在 并且不可用，就设置为 可用
                    $data = VipChannelUsers::updateItem($vipChannel->id, compact('is_enabled'));
                }
            } else {
                $data = VipChannelUsers::addItem(compact('is_enabled', 'vip_manager_id', 'channel_user_id'));
            }
            if ($data) {
                return response()->success();
            } else {
                return response()->error('HANDLE_FAILED');
            }
        }
    }
}